package org.hzz.api.coupon.controller;

import io.gitee.pkmer.convention.controller.BaseController;
import io.gitee.pkmer.convention.page.PageResponse;
import io.gitee.pkmer.convention.result.Result;
import io.gitee.pkmer.security.context.AppContextHolder;
import lombok.Setter;
import org.hzz.api.coupon.UserCouponApi;
import org.hzz.application.command.user.ReceiveCouponCmd;
import org.hzz.application.command.user.UserCouponPageCmd;
import org.hzz.application.service.PromotionsCmdService;
import org.hzz.promotions.types.req.UserCouponPageReq;
import org.hzz.promotions.types.vo.UserCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/2
 */
@RestController
public class UserCouponController extends BaseController implements UserCouponApi {

    @Setter(onMethod_ = @Autowired)
    private PromotionsCmdService cmdService;

    @Override
    public Result<Void> receiveByCouponId(String id) {
        logger.info("通过id = {} 领取优惠券", id);
        Long userId = AppContextHolder.userContextHolder.getUser().getId();
        new ReceiveCouponCmd()
                .setUserId(userId)
                .setCouponId(Long.parseLong(id))
                .execute(cmdService);
        return success();
    }

    @Override
    public Result<Void> receiveByCouponCode(String code) {
        logger.info("通过兑换码 = {} 领取优惠券", code);
        Long userId = AppContextHolder.userContextHolder.getUser().getId();
        new ReceiveCouponCmd()
                .setUserId(userId)
                .setCode(code)
                .execute(cmdService);
        return success();
    }

    @Override
    public Result<PageResponse<UserCouponVo>> page(UserCouponPageReq pageReq) {
        logger.info("分页查询用户领取优惠券");
        Long userId = AppContextHolder.userContextHolder.getUser().getId();

        PageResponse<UserCouponVo> page = UserCouponPageCmd
                .commandOf(pageReq, userId)
                .executeWithResult(cmdService);

        return success(page);
    }
}
